package com.langrensha.langrensha;

import com.langrensha.langrensha.entity.Result;
import com.langrensha.langrensha.entity.Room;
import com.langrensha.langrensha.entity.UserInfo;
import com.langrensha.langrensha.store.RoomStore;
import com.langrensha.langrensha.store.UserStore;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import java.util.*;

@RestController
@RequestMapping("/langren")
public class RoomController {

    public static final String HOST_ID = "hostId";

    /**
     * 加入房间时初始化房间信息。
     *
     * @param room 房间信息ROOM
     * @return result
     */
    @PostMapping("/room")
    public Result room(@RequestBody Room room) {

        Integer hostId = RoomStore.save(room);
        Map<String, Integer> map = new HashMap<>();
        map.put(HOST_ID, hostId);
        return Result.success(map);
    }

    @GetMapping("/room/{hostId}")
    public Result getRoom(@PathVariable("hostId") String hostId) {

        Room room = RoomStore.getRoom(hostId);
        if (room != null) {
            return Result.success(room);
        }
        return new Result().code(404).msg("Resource not exists!");
    }

    @GetMapping("/user")
    public Result createAliasName(@RequestParam("aliasName") String name) {
        if (UserStore.addUserAlias(name)) {
            return Result.success(null);
        }
        return new Result().code(404).msg("Resource is exists!");
    }

    @GetMapping("/test")
    public String config() {
        return "success";
    }

    @GetMapping("/index")
    public ModelAndView index() {
        return new ModelAndView("index");
    }

    @GetMapping("/socket")
    public ModelAndView html() {
        return new ModelAndView("WebsocketChatClient");
    }

    @GetMapping("/WebsocketChatClient")
    public String WebsocketChatClient() {
        return "WebsocketChatClient";
    }

    @GetMapping("/role")
    public String getRole(
            @RequestParam("hostId") String hostId, @RequestParam("name") String name) {
        UserInfo u = UserStore.getByName(hostId, name);
        if (u != null && u.getRole() != null) {
            if (Integer.parseInt(u.getRole()) >= 30) {
                return "你的身份是狼";
            } else if (Integer.parseInt(u.getRole()) >= 10 && Integer.parseInt(u.getRole()) <= 20) {
                return "你的身份是村民";
            }
            return "你的身份是" + RoomStore.role.get(Integer.valueOf(u.getRole()));
        }
        return "未获取到身份信息";
    }
    //
    //    @GetMapping("/save")
    //    public String save(@RequestParam("hostId") String hostId, @RequestParam("name") String
    // name) {
    //        UserInfo u = UserStore.getByName(hostId, name);
    //        if (u != null && u.getRole() != null) {
    //            return "你的身份是" + RoomStore.role.get(Integer.valueOf(u.getRole()));
    //        }
    //        return "未获取到身份信息";
    //    }
    //
    //    @GetMapping("/save")
    //    public String nightInfo(
    //            @RequestParam("hostId") String hostId, @RequestParam("name") String name) {
    //        UserInfo u = UserStore.getByName(hostId, name);
    //        if (u != null && u.getRole() != null) {
    //            return "你的身份是" + RoomStore.role.get(Integer.valueOf(u.getRole()));
    //        }
    //        return "未获取到身份信息";
    //    }
    //
    //    @GetMapping("/save")
    //    public String poison(@RequestParam("hostId") String hostId, @RequestParam("name") String
    // name) {
    //        UserInfo u = UserStore.getByName(hostId, name);
    //        if (u != null && u.getRole() != null) {
    //            return "你的身份是" + RoomStore.role.get(Integer.valueOf(u.getRole()));
    //        }
    //        return "未获取到身份信息";
    //    }
}
